<template>
  <el-dialog
    title="修改商品名称"
    :visible.sync="UpdateGoodsNameDialog"
    :close-on-click-modal="false"
    width="30%"
    :before-close="handleClose"
    :modal-append-to-body="false"
  >
    <el-form ref="elForm" :model="formData" :rules="rules" label-width="110px">
      <el-form-item label="原商品名称：" prop="oldName">
        <el-input
          v-model="formData.oldName"
          :disabled="true"
          placeholder="请输入商品名称50字以内"
        />
      </el-form-item>
      <el-form-item label="现商品名称：" prop="name">
        <el-input v-model="formData.name" placeholder="请输入商品名称50字以内" />
      </el-form-item>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button type="primary" @click="handleConfirm">确定</el-button>
      <el-button type="info" plain @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    UpdateGoodsNameDialog: {
      get() {
        return this.value
      },
      set(value) {
        this.$emit('input', value)
      }
    }
  },
  data() {
    return {
      formData: {
        oldName: '',
        name: '',
        id: ''
      },
      rules: {
        oldName: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur'
          },
          {
            max: 50,
            message: '商品名称在50字以内',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    //初始化数据
    initData(row) {
      this.formData.oldName = row.name
      this.formData.id = row.id
    },
    //确认
    handleConfirm() {
      this.$refs.elForm.validate(async valid => {
        if (valid) {
          try {
            const { status } = await this.$http.goods.goods.updateGoodsName(this.formData)
            if (status === 200) {
              this.$emit('update')
              this.UpdateGoodsNameDialog = false
            }
          } catch (error) {
            console.log(error)
          }
        } else {
          this.$message.warning('有必填项未填请检查')
        }
      })
    },
    //关闭
    handleClose() {
      this.UpdateGoodsNameDialog = false
    }
  }
}
</script>

<style></style>
